package com.ruoyi.exam.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.exam.domain.entity.exentity.PeopleTitleSummary;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.exam.domain.TPeopleTitle;
import com.ruoyi.exam.service.ITPeopleTitleService;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;

/**
 * 人员题库关系Controller
 * 
 * @author ruoyi
 * @date 2025-01-08
 */
@RestController
@RequestMapping("/exam/title")
public class TPeopleTitleController extends BaseController {
    @Autowired
    private ITPeopleTitleService tPeopleTitleService;

    /**
     * 查询人员题库关系列表
     */
    @PreAuthorize("@ss.hasPermi('exam:title:list')")
    @GetMapping("/list")
    public TableDataInfo list(TPeopleTitle tPeopleTitle) {
        // 处理peopleType参数，将逗号分隔的字符串转为List
        if (StringUtils.isNotEmpty(tPeopleTitle.getPeopleType())) {
            tPeopleTitle.setPeopleTypeList(Arrays.asList(tPeopleTitle.getPeopleType().split(",")));
        }
        startPage();
        List<TPeopleTitle> list = tPeopleTitleService.selectTPeopleTitleList(tPeopleTitle);
        return getDataTable(list);
    }

    /**
     * 通过POST方法查询人员题库列表（解决大量参数导致URI过长问题）
     */
    @PostMapping("/listByPost")
    @PreAuthorize("@ss.hasPermi('exam:title:list')")
    public TableDataInfo listByPost(@RequestBody TPeopleTitle tPeopleTitle) {
        // 处理peopleType参数，将逗号分隔的字符串转为List
        if (StringUtils.isNotEmpty(tPeopleTitle.getPeopleType())) {
            tPeopleTitle.setPeopleTypeList(Arrays.asList(tPeopleTitle.getPeopleType().split(",")));
        }
        PageUtils.startPage(1, 10000);
        List<TPeopleTitle> list = tPeopleTitleService.selectTPeopleTitleList(tPeopleTitle);
        return getDataTable(list);
    }

    /**
     * 导出人员题库关系列表
     */
    @PreAuthorize("@ss.hasPermi('exam:title:export')")
    @Log(title = "人员题库关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TPeopleTitle tPeopleTitle) {
        List<TPeopleTitle> list = tPeopleTitleService.selectTPeopleTitleList(tPeopleTitle);
        ExcelUtil<TPeopleTitle> util = new ExcelUtil<TPeopleTitle>(TPeopleTitle.class);
        util.exportExcel(response, list, "人员题库关系数据");
    }

    /**
     * 获取人员题库关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('exam:title:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tPeopleTitleService.selectTPeopleTitleById(id));
    }

    /**
     * 新增人员题库关系
     */
    @PreAuthorize("@ss.hasPermi('exam:title:add')")
    @Log(title = "人员题库关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TPeopleTitle tPeopleTitle) {
        return toAjax(tPeopleTitleService.insertTPeopleTitle(tPeopleTitle));
    }

    /**
     * 修改人员题库关系
     */
    @PreAuthorize("@ss.hasPermi('exam:title:edit')")
    @Log(title = "人员题库关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TPeopleTitle tPeopleTitle) {
        return toAjax(tPeopleTitleService.updateTPeopleTitle(tPeopleTitle));
    }

    /**
     * 删除人员题库关系
     */
    @PreAuthorize("@ss.hasPermi('exam:title:remove')")
    @Log(title = "人员题库关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tPeopleTitleService.deleteTPeopleTitleByIds(ids));
    }
    @GetMapping("/peopleTitleSummary")
    public TableDataInfo list() {
//        startPage();
        List<PeopleTitleSummary> list = tPeopleTitleService.selectPeopleTitleSummaryList();
        return getDataTable(list);
    }
}
